package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.application.MyApplication;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.base.SearchBaseActivity;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.user.activity.CheckHelpActivity;
import com.asktgapp.user.activity.FreeAskActivity;
import com.asktgapp.user.activity.MessageCenterActivity;
import com.asktgapp.user.activity.MyPublishActivity;
import com.asktgapp.user.activity.PublishQiuzuActicity;
import com.asktgapp.user.activity.PublishRentActicity;
import com.asktgapp.user.activity.PublishUsedMachineActivity;
import com.asktgapp.user.activity.UsedCheckActivity;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/17
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description: 首页-发布fragment
 */

public class PublishFragment extends BaseFragment {
    @InjectView(R.id.tv_my_publish)
    TextView myPublish;
    @InjectView(R.id.tv_publish_item1)
    TextView item1;
    @InjectView(R.id.tv_publish_item2)
    TextView item2;
    @InjectView(R.id.tv_publish_item3)
    TextView item3;
    @InjectView(R.id.tv_publish_item4)
    TextView item4;
    @InjectView(R.id.tv_publish_item5)
    TextView item5;
    @InjectView(R.id.tv_publish_item6)
    TextView item6;
    @InjectView(R.id.et_search)
    EditText mSearch;
    @InjectView(R.id.iv_message)
    ImageView mMessage;
    private double unRead;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_publish, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        myPublish.setOnClickListener(this);
        item1.setOnClickListener(this);
        item2.setOnClickListener(this);
        item3.setOnClickListener(this);
        item4.setOnClickListener(this);
        item5.setOnClickListener(this);
        item6.setOnClickListener(this);
        mSearch.setOnClickListener(this);
        mMessage.setOnClickListener(this);

    }

    @Override
    public void loadData() {
        Apiservice apiservice = ApiUtil.getInstance().create();
//        if(!MyApplication.userIdIsEmpty(getContext())){
//            HashMap param = new HashMap();
//            param.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
//            Call<ApiResponseBody> call1 = apiservice.getNotReadMsg(param);
//            call1.enqueue(new Callback<ApiResponseBody>() {
//                @Override
//                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
//                    if (response.isSuccessful()) {
//                        if (response.body().getResult() != null)
//                            unRead = (double) response.body().getResult();
//                        if (unRead > 0) {
//                            mMessage.setImageResource(R.mipmap.xiaoxi_has_new);
//                        } else {
//                            mMessage.setImageResource(R.mipmap.xiaoxi_no_new);
//                        }
//                    } else {
//                        showTost(response.raw().message(), 0);
//                    }
//                }
//
//                @Override
//                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
//                    ApiException apiException = ExceptionHandle.handleException(t);
//                    if (apiException.isTokenInvalid()) {
//                        showUnLoginSnackbar();
//                    } else if (apiException.isNetConnected()) {
////                    showSetNetworkSnackbar();
//                    } else {
////                    showTost(apiException.getMessage(), 1);
//                    }
//
//                }
//            });
//        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(MyApplication.userIdIsEmpty(getContext())){
            showUnLoginSnackbar();
            return;
        }

        switch (v.getId()) {
            //我的发布
            case R.id.tv_my_publish:
                Intent intent = new Intent(getActivity(), MyPublishActivity.class);
                intent.putExtra("type",0);
                startActivity(intent);
                break;
            //二手机
            case R.id.tv_publish_item1:
                startActivity(new Intent(getActivity(), PublishUsedMachineActivity.class));
                break;
            //设备出租
            case R.id.tv_publish_item2:
                startActivity(new Intent(getActivity(), PublishRentActicity.class));
                break;
            //设备求租
            case R.id.tv_publish_item3:
                startActivity(new Intent(getActivity(), PublishQiuzuActicity.class));
                break;
            //二手机检测
            case R.id.tv_publish_item4:
                startActivity(new Intent(getActivity(), UsedCheckActivity.class));
                break;
            //故障代检
            case R.id.tv_publish_item5:
                startActivity(new Intent(getActivity(), CheckHelpActivity.class));
                break;
            //免费问答
            case R.id.tv_publish_item6:
                startActivity(new Intent(getActivity(), FreeAskActivity.class));
                break;
            //消息中心
            case R.id.iv_message:
                startActivity(new Intent(getActivity(), MessageCenterActivity.class));
                break;
            case R.id.et_search:
                startActivity(new Intent(getActivity(), SearchBaseActivity.class));
                break;
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }
}
